<?php
include_once($config['webroot']."/module/product/includes/plugin_pro_class.php");
	$id=!empty($_GET["id"])?$_GET["id"]*1:NULL;
	$key=!empty($_GET["key"])?trim($_GET["key"]):NULL;
	$firstRow=!empty($_GET["firstRow"])?$_GET["firstRow"]:NULL;
	$totalRows=!empty($_GET["totalRows"])?$_GET["totalRows"]:NULL;
	$orderby=!empty($_GET["orderby"])?$_GET['orderby']*1:NULL;
	//===================================分类
	if(is_numeric($id))
	{
		if(strlen($id)>8)
			$catname[]=substr($id,0,-6);
		if(strlen($id)>6)
			$catname[]=substr($id,0,-4);
		if(strlen($id)>4)
			$catname[]=substr($id,0,-2);
		if(strlen($id) == 4)
			$catname[]= $id;
		$tpl->assign("catname",$catname);
		$sql="select * from ".PCAT." where catid='$id'";
		$db->query($sql);
		$cat=$db->fetchRow();
		//=============子分类============================
		$sub_sql = "select * from ".PCAT." where catid >= ".$catname[0].'01'." and catid <= ".$catname[0].'99';
		$db->query($sub_sql);
		$sub_cat = $db->getRows();
		$tpl->assign("sub_cat",$sub_cat);
		//-----------------------------分类关连的品牌
		if(!empty($cat['brand']))
		{
			$sql="select * from ".BRAND." where id in ( $cat[brand] ) order by displayorder asc ";
			$db->query($sql);
			$re=$db->getRows();
			$tpl->assign("brand",$re);
		}

		//-----------------------------获取分类下自定义字段搜索项
		$sql="select display_type,field,field_name,item from ".PROPERTYVALUE." where is_search=1 and display_type in (3,4,5) and property_id='$cat[ext_field_cat]'";
		$db->query($sql);
		$catfile=$db->getRows();
		foreach($catfile as $fkey=>$v)
		{
			$catField=explode(',',$v['item']);
			foreach($catField as $skey=>$sv)
			{
				$catField[$skey]=explode('|',$sv);
			}
			$catfile[$fkey]['catItem']=$catField;
			//------组合皖搜索
			if(!empty($_GET[$v['field']]))
			{
				$ext_sql.=' and b.'.$v['field'].'=\''.$_GET[$v['field']].'\'';
			}
		}
		$tpl->assign("catfile",$catfile);
		//---------------------------------按分类搜索
		$scl.=" and LOCATE(".intval(trim($_GET['id'])).",a.catid)=1 ";//按类别搜索
	}
//=================================分类==================
$sql="select * from ".PCAT." where isindex='1' and catid < 9999 ";
$db->query($sql);
$cat_list = $db->getRows();
$tpl->assign('cat_list',$cat_list);
	if(!empty($key)){
		$db->query("select * from ".SPOT." where spot_name like '%$key%' ");
		$spot_id = $db->fetchField('id');
		$district_id = getdistrictlike($key);
		$scl.=" and ( a.pname like '%$key%' or a.artist like '%$key%' or price = '$key' )";
		if(!empty($spot_id)){
			$scl .= " or a.spot = '$spot_id' ";
		}
		if(!empty($district_id)){
			$scl .= " or a.province = '$district_id' or a.city = '$district_id'  ";
		}
	}
	$get_city = $_GET['city'];
	$get_province = $_GET['province'];
    $tpl->assign("get_city",!empty($get_city) ? $get_city : $get_province );
	$tpl->assign("is_province",!empty($get_province) ? 1 : 0 );
	if($get_province)
		$scl.=" and a.province='".$get_province."'";
	if($get_city){
		$scl.=" and a.city='".$get_city."'";
	}

	if($daid)
		$scl.=" and a.areaid='".getdistrictid($daid)."'";

	if(!empty($_GET['brand']))
		$scl.=" and a.brand='".$_GET['brand']."' ";
	if(!empty($_GET['spot_id'])){
		$scl .= " and a.spot = '$_GET[spot_id]' ";
	}

	if(!empty($_GET['stock']))
		$scl.=" and a.stock > 0 ";
	$scl .= " group by a.pid ";
	if($orderby==2)
		$scl.=" order by a.goodbad desc,a.rank desc";
	elseif($orderby==3)
		$scl.=" order by a.uptime desc,a.rank desc";
	elseif($orderby==4)
		$scl.=" order by a.price desc,a.rank desc";
	elseif($orderby==5)
		$scl.=" order by a.price asc,a.rank desc";
	else
		$scl.="   order by a.sell_amount desc,a.rank desc";

	//--------------------------------------------------

	include_once("includes/page_utf_class.php");
	$page = new Page;
	$page->url=$config['weburl'].'/';
	$page->listRows= 35;
//var_dump($scl);exit;

	if(empty($cat['ext_field_cat']))
		$sql="SELECT a.* FROM ".SETMEAL." a WHERE a.statu>=0 $scl $ext_sql";
	else
		$sql="SELECT a.* FROM ".SETMEAL." a left join ".$cat['ext_table']." b on a.pid=b.info_id and b.info_type='product' WHERE a.statu>=0 $scl $ext_sql  ";
	if(!$page->__get('totalRows'))
	{
		$db->query($sql);
		$page->totalRows =$db->num_rows();
	}
	$sql.=" limit ".$page->firstRow.", ".$page->listRows;
	//--------------------------------------------------
	$db->query($sql);
	$prol=$db->getRows();
	$prodetail=new pro();

	foreach($prol as $key=>$val)
	{
		if($val['statu'])
		{
			$status=explode(',',$val['statu']);
			$sql="select logo from ".TAG." where id ='$status[0]' ";
			$db->query($sql);
			$prol[$key]['simg']=$db->fetchField('logo');
		}
		if(!empty($val['setmeal'])){
			$setmeal = explode(',',$val['setmeal']);
		}
		$prode=$prodetail->detail($val['id']);
		$prol[$key]['spot'] = $prode['spot'];
		$prol[$key]['reserve_time'] = $prode['reserve_time'];
		$prol[$key]['sell_time'] = $prode['sell_time'];
		$prol[$key]['discount'] = $prode['discount'];
	}

	$prolist['list']=$prol;
	$prolist['page']=$page->get_pc_prompt();
	$prolist['count']=$page->totalRows;
    foreach($prolist['list'] as $key => $val){
		$osql = "select price from ".SETMEAL." where pid = '{$val['pid']}' ";
		$db->query($osql);
		$o_list = $db->getRows();
		$o_list = array_unique(array_column($o_list,'price'));

		$max =array_search(max($o_list),$o_list);

		$min =array_search(min($o_list),$o_list);
		$prolist['list'][$key]['price_list'] = $o_list[$min].'-'.$o_list[$max];

	}

	$tpl->assign("info",$prolist);

	//------------------------------------------------------
	$url=implode('&',convert($_GET));
	$tpl->assign("url",$url);


//=============================================城市分站 start==========================================
$domain_sql = "select * from ".DOMAIN." where isopen = '1' ";
$db->query($domain_sql);
$domain_city = $db->getRows();
$all_city = [];
foreach($domain_city as $key => $val) {
	if(!empty($val['con2'])){
		$city_sql = "select * from ".DISTRICT." where name like '%{$val['con2']}%' ";
		$db->query($city_sql);
		$city_info = $db->fetchRow();
		$city_info['is_city'] = 1;
		$city_info['name'] = str_replace("市","",$city_info['name']);
	} else {
		$city_sql = "select * from ".DISTRICT." where name like '%{$val['con']}%' ";
		$db->query($city_sql);
		$city_info = $db->fetchRow();
		$city_info['is_city'] = 0;
		$city_info['name'] = str_replace("市","",$city_info['name']);
	}
	$all_city[$city_info['id']] = $city_info;
}


$tpl->assign("all_city",$all_city);
//=============================================城市分站 end==========================================



	//----------------------------SEO
	$config['title']=str_replace('[catname]',$cat['cat'],$config['title2']);
	$config['keyword']=str_replace('[catname]',$cat['cat'],$config['keyword2']);
	$config['description']=str_replace('[catname]',$cat['cat'],$config['description2']);

	//=====================================================
	$current=$cat['current']?$cat['current']:"product";
	$tpl->assign("current",$current);
    $tpl->assign("cat",$cat);
	$tpl->assign("orderby",$orderby);
	$tpl->assign("id",$id);
	include_once("footer.php");
	$out=tplfetch("product_list.htm");
?>